/*
 * Copyright (C), 2002-2016, 上海亦寻信息技术有限公司
 * FileName: PinController.java
 * Author:   linyao
 * Date:     2016-6-24 下午3:45:18
 * Description: //模块目的、功能描述      
 * History: //修改记录
 * <author>      <time>      <version>    <desc>
 * 修改人姓名             修改时间            版本号                  描述
 */
package com.emm.yixun.website.controller.marketing;

import com.emm.yixun.common.model.Project;
import com.emm.yixun.common.model.RoleResources;
import com.emm.yixun.common.model.User;
import com.emm.yixun.common.model.dailySellDto.DailySellReportRequestDto;
import com.emm.yixun.common.model.dailySellDto.DailySellReportResponseDto;
import com.emm.yixun.common.model.dto.ProjectDto;
import com.emm.yixun.common.model.dto.RoleResourcesDto;
import com.emm.yixun.common.model.dto.UserDto;
import com.emm.yixun.common.model.dto.UserResourcesDto;
import com.emm.yixun.common.model.enums.DelFlagEnum;
import com.emm.yixun.common.response.Response;
import com.emm.yixun.common.response.ResponseList;
import com.emm.yixun.estate.service.ProjectServiceFacade;
import com.emm.yixun.marketing.service.DailySellReportServiceFacade;
import com.emm.yixun.system.service.RoleResourcesServiceFacade;
import com.emm.yixun.system.service.UserServiceFacade;
import com.emm.yixun.website.common.ajax.AjaxRes;
import com.emm.yixun.website.constant.ConstantConfig;
import com.emm.yixun.website.controller.base.BaseController;
import com.emm.yixun.website.service.ao.DataService.OaDataService;
import com.emm.yixun.website.vo.oa.OaUserData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 〈一句话功能简述〉 〈功能详细描述〉
 * 
 * @author linyao
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Controller
@RequestMapping("marketing/dailySellReport")
public class DailySellReportController extends BaseController<Object>{

	@Autowired
	private DailySellReportServiceFacade dailySellReportServiceFacade;
	@Autowired
	private OaDataService oaDataService;
	@Autowired
	private UserServiceFacade userServiceFacade;
	@Autowired
	private ProjectServiceFacade projectServiceFacade;
	@Autowired
	private RoleResourcesServiceFacade roleResourcesServiceFacade;


	@RequestMapping("projectDailySell")
	public @ResponseBody AjaxRes projectDailySell(DailySellReportRequestDto reqDto) {
		AjaxRes res = new AjaxRes();
		List<DailySellReportResponseDto> resList = dailySellReportServiceFacade.getDailySellReportResponse(reqDto);
		res.setSucceed(resList);
		return res;

	}

	@RequestMapping("projectWholeSell")
	public @ResponseBody AjaxRes projectWholeSell(DailySellReportRequestDto reqDto) {
		AjaxRes res = new AjaxRes();
		List<DailySellReportResponseDto> resList = dailySellReportServiceFacade.getWholeSellReportResponse(reqDto);
		res.setSucceed(resList);
		return res;

	}

	/**
	 * 微信销售日报表: 获取用户权限下的房源
	 * @param userId 用户Id
	 * @param userticket 用户登录凭证
	 * @return
	 */
	@RequestMapping("/queryOaUserData")
	public @ResponseBody
	Response<OaUserData> queryOaUserResources(String userId, String userticket) {
		Response<OaUserData> response = new Response<OaUserData>();
		Map<String, Boolean> returnMap = new HashMap<String, Boolean>();
		HashMap<String, String> hashMap = new HashMap<String, String>();
		//当ticket为9999_JHsong时,userId为用户在系统中的绿城oaUserCode
		if("9999_JHSong".equals(userticket)){
			hashMap.put("oaId", userId);
		}else{
			hashMap = oaDataService.getOaId(userId, userticket);
		}

		if (null == hashMap || !StringUtils.isEmpty(hashMap.get("error"))) {
			response.setSuccess(false);
			response.setResultMessage("获取用户失败");
		} else if (!StringUtils.isEmpty(hashMap.get("oaId"))) {
			OaUserData oaUserData = new OaUserData();
			String oaId = hashMap.get("oaId");
			UserDto userDto = new UserDto();
			userDto.setOaUserCode(oaId);
			ResponseList<User> userResList = userServiceFacade.findByDto(userDto);
			if(userResList.isSuccess() && null != userResList.getEntity() && userResList.getEntity().size() > 0){
				User user = userResList.getEntity().get(0);
				oaUserData.setUser(user);
				oaUserData.setResourcesCode("menu_0009_00020");
				oaUserData.setRoleId(user.getRoleId());
				oaUserData.setFlag(false);
				//校验角色是否存在该资源
				RoleResourcesDto roleResourcesDto = new RoleResourcesDto();
				roleResourcesDto.setRoleId(user.getRoleId());
				roleResourcesDto.setResourcesCode("menu_0009_00020");
				roleResourcesDto.setDelFlag(DelFlagEnum.UNDEL.getNo());
				ResponseList<RoleResources> roleResourcesResponseList = roleResourcesServiceFacade.findByDto(roleResourcesDto);
				if(roleResourcesResponseList.isSuccess()){
					if(null != roleResourcesResponseList.getEntity() && roleResourcesResponseList.getEntity().size() > 0){
						oaUserData.setFlag(true);
					}
				}
				response.setSuccess(true);
				response.setEntity(oaUserData);
			}else{
				return response;
			}
		}
		return response;
	}

	/**
	 * 微信销售日报表: 获取用户权限下的房源
	 * @param userId 用户Id
	 * @return
	 */
	@RequestMapping("/queryOaProject")
	public @ResponseBody ResponseList<Project> queryOaProject(Long userId) {
		ResponseList<Project> responseList = new ResponseList<Project>();
		if(null != userId){
			Response<User> userRes = userServiceFacade.findById(userId);
			if(userRes.isSuccess() && null != userRes.getEntity()){
				User user = userRes.getEntity();
				HashMap<String, String> hashMap = new HashMap<String, String>();
				ProjectDto projectDto = new ProjectDto();
				projectDto.setMerchantId(user.getMerchantId());
				projectDto.setDelFlag(DelFlagEnum.UNDEL.getNo());
				projectDto.setResourcesCode("menu_0003_0001");
				projectDto.setPermissionUserId(userId);
				responseList = projectServiceFacade.findByDto(projectDto);
			}
		}
		return responseList;
	}
	
}
